﻿using System;
using System.Windows;
using Microsoft.Phone.Shell;
using System.Windows.Data;
using MyDeeWeibo;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using MyDeeWeibo.Classes;
using Coding4Fun.Phone.Controls;

namespace DeeWeibo
{
    public partial class ImageShow : PhoneApplicationPage
    {
        #region ProgressIndicatorIsVisibleProperty
        public static readonly DependencyProperty ProgressIndicatorIsVisibleProperty =
            DependencyProperty.Register("ProgressIndicatorIsVisible",
            typeof(bool),
            typeof(ImageShow),
            new PropertyMetadata(false));

        public bool ProgressIndicatorIsVisible
        {
            get { return (bool)GetValue(ProgressIndicatorIsVisibleProperty); }
            set { SetValue(ProgressIndicatorIsVisibleProperty, value); }
        }
        #endregion

        #region GifPicShowProperty
        public static readonly DependencyProperty GifPicShowProperty =
            DependencyProperty.Register(
            "GifPicShow",
            typeof(string),
            typeof(ImageShow),
            new PropertyMetadata((string)null));

        public string GifPicShow
        {
            get { return (string)GetValue(GifPicShowProperty); }
            set { SetValue(GifPicShowProperty, value); }
        }
        #endregion

        //double initialAngle;
        double initialScale;
        BitmapImage _bitMapImage = new BitmapImage();

        public static string OriginalImageShow;
        public bool changeSize;

        public ImageShow()
        {
            changeSize = true;
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            SystemTray.ProgressIndicator = new ProgressIndicator();
            SystemTray.ProgressIndicator.Text = "图片加载中";

            Binding bindingData;
            bindingData = new Binding("ProgressIndicatorIsVisible");
            bindingData.Source = this;
            BindingOperations.SetBinding(SystemTray.ProgressIndicator, ProgressIndicator.IsVisibleProperty, bindingData);
            BindingOperations.SetBinding(SystemTray.ProgressIndicator, ProgressIndicator.IsIndeterminateProperty, bindingData);
            ProgressIndicatorIsVisible = true;

            if (WeiboSettings.StatusResult.original_pic != null)
            {
                ShowAppropriatePic(WeiboSettings.StatusResult.original_pic);
            }
            else
            {
                ShowAppropriatePic(WeiboSettings.StatusResult.retweeted_status.original_pic);
            }
        }

        private void ShowAppropriatePic(string picUri)
        {
            if (ExtHelpers.isGifImage(picUri))
            {
                GifPicShow = picUri;
                orgPic.Visibility = Visibility.Collapsed;
                transform.TranslateX = transform.TranslateY = 0;
                double aa = this.orgPic.ActualHeight;
            }
            else
            {
                OriginalImageShow = picUri;
                Uri uriSource = new Uri(OriginalImageShow);
                orgPic.Source = new StorageCachedImage(uriSource);
                GifShow.Visibility = Visibility.Collapsed;
                double aa = this.orgPic.ActualHeight;
            }
        }

        private void orgPic_SizeChanged(object sender, RoutedEventArgs e)
        {
            //if (this.orgPic.ActualHeight != 0 || this.orgPic.ActualWidth != 0)
            {
                ProgressIndicatorIsVisible = false;
            }
        }

        private void GifShow_SizeChanged(object sender, RoutedEventArgs e)
        {
            if (this.GifShow.ActualHeight != 0)
            {
                ProgressIndicatorIsVisible = false;
            }
        }

        #region NormalPic

        private void OnTap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {

        }

        private void OnDoubleTap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            if (changeSize)
            {
                transform.ScaleX *= 2;
                transform.ScaleY *= 2;
                transform.TranslateX = 0;
                transform.TranslateY = 0;
                changeSize = false;
            }
            else
            {
                transform.ScaleX *= 0.5;
                transform.ScaleY *= 0.5;
                transform.TranslateX = transform.TranslateY = 0;
                changeSize = true;
            }
        }

        private void OnHold(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            //transform.TranslateX = transform.TranslateY = 0;
            //transform.ScaleX = transform.ScaleY = 1;
            //transform.Rotation = 0;
        }

        private void OnDragStarted(object sender, DragStartedGestureEventArgs e)
        {
            //border.Background = greenBrush;
        }

        private void OnDragDelta(object sender, DragDeltaGestureEventArgs e)
        {
            transform.TranslateX += e.HorizontalChange;
            transform.TranslateY += e.VerticalChange;
        }

        private void OnDragCompleted(object sender, DragCompletedGestureEventArgs e)
        {
            //border.Background = normalBrush;
        }

        private void OnPinchStarted(object sender, PinchStartedGestureEventArgs e)
        {
            //border.Background = redBrush;

            //initialAngle = transform.Rotation;
            initialScale = transform.ScaleX;
        }

        private void OnPinchDelta(object sender, PinchGestureEventArgs e)
        {

            //transform.Rotation = initialAngle + e.TotalAngleDelta;
            if (transform.ScaleX > 0.5 && transform.ScaleY > 0.5)
            {
                transform.ScaleX = transform.ScaleY = initialScale * e.DistanceRatio;
            }
        }

        private void OnPinchCompleted(object sender, PinchGestureEventArgs e)
        {
            // _image.Background = normalBrush;
        }

        private void OnFlick(object sender, FlickGestureEventArgs e)
        {
            //flickData.Text = string.Format("{0} Flick: Angle {1} Velocity {2},{3}",
            // e.Direction, Math.Round(e.Angle), e.HorizontalVelocity, e.VerticalVelocity);
        }

        #endregion

        #region GifPic

        private void OnTapGif(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            //transform.TranslateX = transform.TranslateY = 0;
        }

        private void OnDoubleTapGif(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            if (changeSize)
            {
                transformgif.ScaleX *= 2;
                transformgif.ScaleY *= 2;
                transformgif.TranslateX = transformgif.TranslateY = 0;
                changeSize = false;
            }
            else
            {
                transformgif.ScaleX *= 0.5;
                transformgif.ScaleY *= 0.5;
                transformgif.TranslateX = transformgif.TranslateY = 0;
                changeSize = true;
            }
        }

        private void OnHoldGif(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            //transform.TranslateX = transform.TranslateY = 0;
            //transform.ScaleX = transform.ScaleY = 1;
            //transform.Rotation = 0;
        }

        private void OnDragStartedGif(object sender, DragStartedGestureEventArgs e)
        {
            //border.Background = greenBrush;
        }

        private void OnDragDeltaGif(object sender, DragDeltaGestureEventArgs e)
        {
            transformgif.TranslateX += e.HorizontalChange;
            transformgif.TranslateY += e.VerticalChange;
        }

        private void OnDragCompletedGif(object sender, DragCompletedGestureEventArgs e)
        {
            //border.Background = normalBrush;
        }

        private void OnPinchStartedGif(object sender, PinchStartedGestureEventArgs e)
        {
            //border.Background = redBrush;

            //initialAngle = transform.Rotation;
            initialScale = transformgif.ScaleX;
        }

        private void OnPinchDeltaGif(object sender, PinchGestureEventArgs e)
        {

            //transform.Rotation = initialAngle + e.TotalAngleDelta;
            if (transformgif.ScaleX > 0.5 && transformgif.ScaleY > 0.5)
            {
                transformgif.ScaleX = transformgif.ScaleY = initialScale * e.DistanceRatio;
            }
        }

        private void OnPinchCompletedGif(object sender, PinchGestureEventArgs e)
        {
            // _image.Background = normalBrush;
        }

        private void OnFlickGif(object sender, FlickGestureEventArgs e)
        {
            //flickData.Text = string.Format("{0} Flick: Angle {1} Velocity {2},{3}",
            // e.Direction, Math.Round(e.Angle), e.HorizontalVelocity, e.VerticalVelocity);
        }

        #endregion

        private void RestoreIconButton_Click(object sender, EventArgs e)
        {
            if (GifPicShow != null)
            {
                transformgif.TranslateX = transformgif.TranslateY = 0;
                transformgif.ScaleX = transformgif.ScaleY = 1;
                transformgif.Rotation = 0;
                changeSize = true;
            }
            else
            {
                transform.TranslateX = transform.TranslateY = 0;
                transform.ScaleX = transform.ScaleY = 1;
                transform.Rotation = 0;
                changeSize = true;
            }

        }

        private void SaveIconButton_Click(object sender, EventArgs e)
        {
            if (GifPicShow != null)
            {
                var messagePrompt = new MessagePrompt
                {
                    Title = "提示",
                    Message = "本系统不支持保存gif图片",
                };
                messagePrompt.Show();
            }
            else if (OriginalImageShow != null)
            {
                Uri savedSource = new Uri(OriginalImageShow);
                new SavePicToPhone(savedSource);
                OriginalImageShow = null;
            }
        }
    }
}
